
import 'dart:convert';
import 'dart:ffi';

import 'package:common_utils/common_utils.dart';
import 'package:fluro/fluro.dart';
import 'package:talk/pages/device/model/Device.dart';
import 'package:talk/pages/device/qr_code_scanner_page.dart';
import 'package:talk/pages/my/model/UserModel.dart';
import 'package:talk/pages/provider/specifics_select.dart';

import '../../routers/i_router.dart';
import '../provider/data_select.dart';
import 'ScanPage.dart';
import 'device_alarm.dart';
import 'device_alarm_defense.dart';
import 'device_alarm_info.dart';
import 'device_alarm_motion.dart';
import 'device_back_page.dart';
import 'device_details_page.dart';
import 'device_info.dart';
import 'device_intercom.dart';
import 'device_lock.dart';
import 'device_more.dart';
import 'device_setting.dart';
import 'device_share.dart';
import 'device_share_add.dart';
import 'device_specifics.dart';
import 'device_storage.dart';
import 'device_version.dart';
import 'model/DeviceAlarm.dart';


class XDeviceRouter implements IRouterProvider{

  static String deivcePage = '/device';
  static String deviceDetails = '/device/deviceDetails';
  static String deviceBackPage = '/device/deviceBackPage';
  static String qrCodeScannerPage = '/device/qrCodeScanner';
  static String deviceSettingPage = '/device/deviceSettingPage';
  static String deviceInfoPage = '/device/deviceInfoPage';
  static String deviceVersionPage = '/device/deviceVersionPage';
  static String deviceSharePage = '/device/deviceSharePage';
  static String deviceShareAddPage = '/device/deviceShareAddPage';
  static String deviceSpecificsPage = '/device/deviceSpecificsPage';
  static String deviceAlarmInfoPage = '/device/deviceAlarmInfoPage';
  static String deviceAlarmMontionPage = '/device/deviceAlarmMontionPage';
  static String deviceAlarmDefensePage = '/device/deviceAlarmDefensePage';
  static String deviceAlarmPage = '/device/deviceAlarmPage';
  static String deviceLockPage = '/device/deviceLockPage';
  static String deviceStoragePage = '/device/deviceStoragePage';
  static String deviceIntercomPage = '/device/deviceIntercomPage';
  static String deviceMorePage = '/device/deviceMorePage';
  static String dataSelectPage = '/device/dataSelectPage';
  static String specificsSelectPage = '/device/specificsSelectPage';
  static String scanPage = '/device/scanPage';


  @override
  void initRouter(FluroRouter router) {
    router.define(deviceDetails, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? typeStr = params['type']?.first;
      final int type = int.tryParse(typeStr ?? '0') ?? 0;

      final String? sumStr = params['sum']?.first;
      final int sum = int.tryParse(sumStr ?? '0') ?? 0;

      final bool isScan = params['isScan']?.first == 'true';

      final bool isSipInitialized = params['isSipInitialized']?.first == 'true';
      final bool isRegistered = params['isRegistered']?.first == 'true';

      return DeviceDetailsPage(
          deviceId: deviceId,
          isScan: isScan,
          type: type,
          sum: sum,
          isSipInitialized: isSipInitialized,
          isRegistered: isRegistered,
      );
    }));


    router.define(deviceBackPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? typeStr = params['type']?.first;
      final int type = int.tryParse(typeStr ?? '0') ?? 0;

      final String? sumStr = params['sum']?.first;
      final int sum = int.tryParse(sumStr ?? '0') ?? 0;

      final bool isScan = params['isScan']?.first == 'true';

      final bool isSipInitialized = params['isSipInitialized']?.first == 'true';
      final bool isRegistered = params['isRegistered']?.first == 'true';

      return DeviceBackPage(
        deviceId: deviceId,
        isScan: isScan,
        type: type,
        sum: sum,
        isSipInitialized: isSipInitialized,
        isRegistered: isRegistered,
      );
    }));


    // router.define(qrCodeScannerPage, handler: Handler(handlerFunc: (_, __) => const QrCodeScannerPage()));
    // router.define(deviceSettingPage, handler: Handler(handlerFunc: (_, __) => const DeviceSettingPage()));

    router.define(deviceSettingPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? typeStr = params['type']?.first;
      final int type = int.tryParse(typeStr ?? '0') ?? 0;

      final String? isShareStr = params['isShare']?.first;
      final int isShare = int.tryParse(isShareStr ?? '0') ?? 0;

      final String deviceName = params['deviceName']?.first ?? '';

      return DeviceSettingPage(deviceId: deviceId, type: type, isShare: isShare, deviceName: deviceName,);
    }));



    router.define(deviceInfoPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      //传递device信息
      final String? deviceJsonStr = params['device']?.first;
      Device? device = Device.fromJson(json.decode(deviceJsonStr!));


      return DeviceInfoPage(deviceId: deviceId, device: device);
    }));



    router.define(deviceVersionPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['device']?.first;
      Device? device = Device.fromJson(json.decode(deviceJsonStr!));

      return DeviceVersionPage(deviceId: deviceId, device: device);
    }));


    router.define(deviceSharePage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? userIdStr = params['userId']?.first;
      final int userId = int.tryParse(userIdStr ?? '0') ?? 0;

      return DeviceSharePage(deviceId: deviceId, userId: userId);
    }));

    router.define(deviceShareAddPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? userIdStr = params['userId']?.first;
      final int userId = int.tryParse(userIdStr ?? '0') ?? 0;

      return DeviceShareAddPage(deviceId: deviceId, userId: userId);
    }));


    router.define(deviceSpecificsPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? shareIdStr = params['shareId']?.first;
      final int shareId = int.tryParse(shareIdStr ?? '0') ?? 0;

      final String? toIdStr = params['toId']?.first;
      final int toId = int.tryParse(toIdStr ?? '0') ?? 0;


      return DeviceSpecificsPage(deviceId: deviceId,
          shareId: shareId, toId: toId);
    }));





    router.define(deviceAlarmInfoPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['deviceAlarm']?.first;
      DeviceAlarm? deviceAlarm = DeviceAlarm.fromJson(json.decode(deviceJsonStr!));

      return DeviceAlarmInfoPage(deviceId: deviceId, deviceAlarm: deviceAlarm);
    }));


    //移动侦测
    router.define(deviceAlarmMontionPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['deviceAlarm']?.first;
      DeviceAlarm? deviceAlarm = DeviceAlarm.fromJson(json.decode(deviceJsonStr!));

      return DeviceAlarmMotionPage(deviceId: deviceId, deviceAlarm: deviceAlarm);
    }));

    //防区设置
    router.define(deviceAlarmDefensePage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['deviceAlarm']?.first;
      DeviceAlarm? deviceAlarm = DeviceAlarm.fromJson(json.decode(deviceJsonStr!));

      return DeviceAlarmDefensePage(deviceId: deviceId, deviceAlarm: deviceAlarm);
    }));







    router.define(deviceAlarmPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['device']?.first;
      Device? device = Device.fromJson(json.decode(deviceJsonStr!));

      return DeviceAlarmPage(deviceId: deviceId, device: device);
    }));


    router.define(deviceLockPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['device']?.first;
      Device? device = Device.fromJson(json.decode(deviceJsonStr!));

      return DeviceLockPage(deviceId: deviceId, device: device);
    }));



    router.define(deviceStoragePage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['device']?.first;
      Device? device = Device.fromJson(json.decode(deviceJsonStr!));

      return DeviceStoragePage(deviceId: deviceId, device: device);
    }));

    router.define(deviceIntercomPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['device']?.first;
      Device? device = Device.fromJson(json.decode(deviceJsonStr!));

      return DeviceIntercomPage(deviceId: deviceId, device: device);
    }));


    router.define(deviceMorePage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? deviceIdStr = params['deviceId']?.first;
      final int deviceId = int.tryParse(deviceIdStr ?? '0') ?? 0;

      final String? isShareStr = params['isShare']?.first;
      final int isShare = int.tryParse(isShareStr ?? '0') ?? 0;

      final String? deviceJsonStr = params['device']?.first;
      Device? device = Device.fromJson(json.decode(deviceJsonStr!));

      return DeviceMorePage(deviceId: deviceId, isShare: isShare, device: device);
    }));




    router.define(dataSelectPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      // 解析camId
      final String? camIdStr = params['camId']?.first;
      final int camId = int.tryParse(camIdStr ?? '0') ?? 0;

      // 解析weekId
      final String? weekIdStr = params['weekId']?.first;
      final int weekId = int.tryParse(weekIdStr ?? '0') ?? 0;

      // 解析week（星期信息）
      final String week = params['week']?.first ?? '';

      // 解析开始时间和结束时间
      final String startTime = params['startTime']?.first ?? '';
      final String endTime = params['endTime']?.first ?? '';

      // 传递所有参数到DataSelectPage
      return DataSelectPage(
        camId: camId,
        weekId: weekId,
        week: week,
        startTime: startTime,
        endTime: endTime,
      );
    }));


    router.define(specificsSelectPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {
      final String? shareIdStr = params['shareId']?.first;
      final int shareId = int.tryParse(shareIdStr ?? '0') ?? 0;

      final String endTime = params['endTime']?.first ?? '';

      return SpecificsSelectPage(shareId: shareId, endTime: endTime);
    }));


    router.define(scanPage, handler: Handler(handlerFunc: (_, Map<String, List<String>> params) {


      return ScanPage();
    }));









  }
}
